package com.itany.book.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.exception.ManagerServiceException;
import com.itany.mapper.PermissionMapper;
import com.itany.mapper.RoleMapper;
import com.itany.pojo.Permission;
import com.itany.pojo.Role;
import com.itany.pojo.RoleExample;
import com.itany.service.RoleService;
import com.itany.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public class RoleServiceImpl implements RoleService {

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    PermissionMapper permissionMapper;

    @Override
    public PageInfo<Role> findAll(Integer page, Integer rows, String name) {
        PageHelper.startPage(page, rows);
        List<Role> list = roleMapper.findAllRoleWithPermissions(name);
        PageInfo<Role> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public List<Role> findAll() {
        List<Role> list = roleMapper.selectByExample(new RoleExample());
        return list;
    }

    @Override
    public void updateRole(Integer id, String name, Integer[] pids) throws ManagerServiceException {
        if (StringUtils.isEmpty(name)) {
            throw new ManagerServiceException("名字为空");
        }

        Role role = new Role();
        role.setId(id);
        role.setName(name);

        roleMapper.updateByPrimaryKey(role);
        roleMapper.deleteRoleAndPermission(id);

        for (int i = 0; i < pids.length; i++) {

            Permission permission = permissionMapper.selectByPrimaryKey(pids[i]);
            if (permission.getParentid() == null || permission.getParentid() == 0) {
                continue; // 模块权限跳过
            }

            try {
                roleMapper.addRoleWithPermission(role.getId(), pids[i]);
            } catch (Exception e) {
                throw new ManagerServiceException("t_role_permission 表插入失败");
            }
        }
    }

    @Override
    public void delete(Integer id) {
        roleMapper.deleteRoleAndPermission(id);
        roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void addRole(String name, Integer[] pids) throws ManagerServiceException {
        if (StringUtils.isEmpty(name)) {
            throw new ManagerServiceException("用户名为空");
        }
        Role role = new Role();
        role.setName(name);
        roleMapper.insert(role);
        for (int i = 0; i < pids.length; i++) {

            Permission permission = permissionMapper.selectByPrimaryKey(pids[i]);
            if (permission.getParentid() == null || permission.getParentid() == 0) {
                continue;
            }

            try {
                roleMapper.addRoleWithPermission(role.getId(), pids[i]);
            } catch (Exception e) {
                throw new ManagerServiceException("t_role_permission 表插入失败");
            }
        }
    }
}
